package com.lq.ecoldchain.ui.goodstype

import android.arch.lifecycle.MutableLiveData
import android.content.Intent
import com.lq.ecoldchain.base.viewmodel.BaseViewModel
import com.lq.ecoldchain.entity.GoodsTypeBean
import com.lq.ecoldchain.internet.BaseObserver
import com.lq.ecoldchain.ui.goodstype.goodsitem.GoodsItemActivity

/*
*2019/1/10 0010  11:49
*function by lq
*/
class GoodsTypeViewModel : BaseViewModel() {
    val refreshing by lazy { MutableLiveData<Boolean>() }
    val list by lazy { ArrayList<GoodsTypeBean>() }
    val adapter by lazy { GoodsTypeRecyclerAdapter(list) }

    init {
        obtainData()
        adapter.setOnItemChildClickListener { _, view, position ->
            view.context.startActivity(Intent(view.context,GoodsItemActivity::class.java).putExtra("data",list[position]))
         /*   val list = item.goodsTypes
            val adapter = GoodsItemRecyclerAdapter(list)
            val view = helper.getView<RecyclerView>(R.id.goodsTypeItemRv)
            view.adapter = adapter*/
        }
    }

    fun refreshData() {
        refreshing.value = true
        obtainData()
    }

    private fun obtainData() {
        BaseNetCore.ApiServiceImpl.obtainGoodsTypeList().compose(RxTransformer.switchSchedulers()).subscribe(object : BaseObserver<List<GoodsTypeBean>>() {
            override fun onSuccess(t: List<GoodsTypeBean>?) {
                if (!t.isNullOrEmpty()) {
                    list.clear()
                    list.addAll(t)
                    adapter.notifyDataSetChanged()
                }
                refreshing.value = false
            }

            override fun onFail(code: String) {
                refreshing.value = false
            }
        })
    }
}